<?php

/**
 * class ScoreKeeper
 * 
 * 01.21.2005
 *  Got class working perfectly.  TODO: test ScoreKeeper.as.
 */
require_once "FlashOutput.php";

/**
 * Score file should be in order of greatest score to least.
 */

define("DELIMITER", ":");

class ScoreKeeper {
	var $ext = "scores";
	var $scores;
	var $orderedScores = array();
	var $separator = ":";
	var $newline = "\r\n";
	var $filename = "";
	function ScoreKeeper($dir_name, $game_name) {
		$this->filename = sprintf("%s%s.scores", $dir_name, $game_name);
		$file = file_get_contents($this->filename);
		$lines = explode($this->newline, $file);
		if(count($lines) > 0) {
			foreach($lines as $line) {
				list($player, $score) = explode($this->separator, $line);
				//echo "Line: ".$line;
				$this->scores[$player] = $score;
				$this->orderedScores[] = array($player, $score); 
			}	
		}
	}
	function writeScores($limit = 10) {
		$file = array();
		$i = 0;
		foreach($this->getScores() as $item) {
			$i++; 
			if($i > $limit) break; 
			list($player, $score) = $item;
			$file[] = $player.$this->separator.$score;
		}
		$file = implode($this->newline, $file);
		$filename = 'test.txt'; 
		// Let's make sure the file exists and is writable first. 
		if (is_writable($this->filename)) { 
			
		   if (!$fp = fopen($this->filename, 'w')) { 
			print "Cannot open file ($this->filename)"; 
			return false;
		   } 

		   // Write $file to our opened file. 
		   if (!fwrite($fp, $file)) { 
		       print "Cannot write to file ($this->filename)"; 
		       return false;
		   } 
		    
		   fclose($fp); 
				    
		} else { 
		   print "The file {$this->filename} is not writable"; 
		   return false;
		} 
		return true;
	}
	function resetScores() {
		$this->orderedScores = array();
	}
	function addScore($player, $score) {
		$item = array($player, $score);
		$newarr = array();
		$inserted = false;
		$this->orderedScores[] = $item;
	}
	/**
	 * only adds score if it is higher than ones in list.
	 */
	function addHighestScore($player, $score) {
		$item = array($player, $score);
		$newarr = array();
		$inserted = false;
		
		for($i=0; $i<count($this->orderedScores); $i++) {
			if(!$inserted && $score > $this->orderedScores[$i][1]) {
				$newarr[] = $item;
				$inserted = true;
			}
			$newarr[] = $this->orderedScores[$i];
		}
		$this->orderedScores = $newarr;
	}
	function &getScores() {
		return $this->orderedScores;
	}
}

if(isset($_REQUEST['game']) && isset($_REQUEST['mode']) && isset($_REQUEST['password'])) {
	$game = $_REQUEST['game'];
	$mode = $_REQUEST['mode'];
	$password = $_REQUEST['password'];
	$rtn = array('success' => 'no');
		
	$sk = new ScoreKeeper("./", $game); // instantiate new score keeper class for tetris
	switch($mode) {
	case "addScores": //if($mode == "addScores") {
		$sk->resetScores();
		if(isset($_REQUEST['scores'])) {
			$scores = $_REQUEST['scores'];
			foreach(explode(',', $scores) as $item) {
				list($player, $score) = explode(':', $item);
				$sk->addScore($player, $score);
			}
			if($sk->writeScores()) {
				$rtn['success'] = "yes";
			} else {
				$rtn['errorMsg'] = "Was not able to write scores.";
			}
		} else {
			$rtn['errorMsg'] = "No scores given to be added.";
		}
	//} elseif ($mode == "getScores") {
	case "getScores":
		$scores = array();
		
		foreach($sk->getScores() as $score) {
			$scores[] = $score[0] . DELIMITER . $score[1];
		}
		$rtn['scores'] = implode(',', $scores);
		$rtn['success'] = 'yes';
		break;
	//} else {
	default:
		$rtn['errorMsg'] = "Unrecognized mode.";
	}

	$fo = new FlashOutput();
	echo $fo->getArrayAsString($rtn);
} else {?>

<html>
	<head><title>ScoreKeeper v0.1</title></head>
	<body>
		<p><span style="font-size:xx-large">ScoreKeeper v 0.1</span><span style="font-size:x-small">by Jamie Ly</span></p>
	</body>
</html>

<?php
}


?>